﻿using System;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using tactics.Interfaces;
using tactics.StatusEffects;

namespace tactics
{
    public class StatusEffectLoader
    {
        private XmlReader reader;
        private List<IStatusEffect> statusEffects;

        #region properties

        public List<IStatusEffect> StatusEffects
        {
            get
            {
                return statusEffects;
            }
        }

        #endregion

        public StatusEffectLoader(string file)
        {
            reader = XmlReader.Create(file);
        }

        public void Load()
        {
            statusEffects = new List<IStatusEffect>();
            reader.MoveToContent();

            while (reader.ReadToFollowing("effect"))
            {
                statusEffects.Add(GetEffect(reader.ReadSubtree()));
            }
        }

        public IStatusEffect GetEffectById(int id)
        {
            IStatusEffect effect = null;

            foreach (IStatusEffect status in statusEffects)
                if (status.Id == id)
                    effect = status.Clone();
            
            return effect;
        }

        private IStatusEffect GetEffect(XmlReader reader)
        {
            reader.ReadToFollowing("id");
            int id = reader.ReadElementContentAsInt("id", reader.NamespaceURI);
            reader.ReadToFollowing("name");
            string name = reader.ReadElementContentAsString("name", reader.NamespaceURI);
            reader.ReadToFollowing("type");
            string type = reader.ReadElementContentAsString("type", reader.NamespaceURI);
            reader.ReadToFollowing("duration");
            int duration = reader.ReadElementContentAsInt("duration", reader.NamespaceURI);
            reader.ReadToFollowing("amount");
            int amount = reader.ReadElementContentAsInt("amount", reader.NamespaceURI);
            reader.ReadToFollowing("damagetype");
            string damageType = reader.ReadElementContentAsString("damagetype", reader.NamespaceURI);
            
            DamageType dt = DamageType.Invalid;
            FieldInfo fi = (dt.GetType()).GetField(damageType);
            if (fi != null)
            {
                dt = (DamageType)Enum.Parse(typeof(DamageType), damageType);
            }

            reader.ReadToFollowing("texture");
            string texture = reader.ReadElementContentAsString("texture", reader.NamespaceURI);

            switch (type)
            {
                case "hp":
                    return new HealthStatusEffect(id, name, duration, amount, dt, texture);
                    
                case "speed":
                    return new SpeedStatusEffect(id, name, duration, amount, texture);

                case "attack":
                    return new AttackStatusEffect(id, name, duration, amount, texture);

                case "magic":
                    return new MagicStatusEffect(id, name, duration, amount, texture);

                case "defense":
                    return new DefenseStatusEffect(id, name, duration, amount, texture);

                case "magicDefense":
                    return new MagicDefenseStatusEffect(id, name, duration, amount, texture);

                case "stun":
                    return new StunStatusEffect(id, name, duration, amount, texture);

                default:
                    return new HealthStatusEffect(id, "emptyEffect", 0, 0, dt, texture);
                    
            }
            
        }
    }
}
